/*
 * ParticipanceManagerBean.java
 *
 * Created on 13. September 2007, 16:52
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package timeitj.ejb.participance;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.NoResultException;
import org.apache.log4j.Logger;
import timeitj.ejb.persistence.PersistenceManagerAbstract;
import timeitj.persistence.Participance;
import timeitj.persistence.Person;
import timeitj.persistence.Project;

/**
 *
 * @author kunee
 */
@Stateless
public class ParticipanceManagerBean extends PersistenceManagerAbstract<Participance, Long> implements ParticipanceManager {
    
    /** Creates a new instance of ParticipanceManagerBean */
    public ParticipanceManagerBean() {
        log = Logger.getLogger(ParticipanceManagerBean.class.getName());
        log.debug("ParticipanceManagerBean instantiated");
    }
    
    public List<Project> getProjectsByParticipant(Person participant) {
        
        List<Project> result;
        try {
            result = (ArrayList<Project>)
            em.createQuery("select p from Project p, in (p.participances) as part where part.person = :person")
            .setParameter("person", participant)
            .getResultList();
        } catch (NoResultException ex) {
            return null;
        }
        
        return result;
    }
    
    public List<Person> getParticipantsByProject(Project project) {
        
        List<Person> result;
        try {
            result = (ArrayList<Person>)
            em.createQuery("select p from Person p, in (p.participances) as part where part.project = :project")
            .setParameter("project", project)
            .getResultList();
        } catch (NoResultException ex) {
            return null;
        }
        
        return result;
    }
    
    public List<Participance> getParticpipancesByPerson(Person person) {
        List<Participance> result;
        
        try {
            result = (ArrayList<Participance>)
                     em.createNamedQuery("ParticipancesByPerson")
                     .setParameter("person", person)
                     .getResultList();
            
        } catch (NoResultException ex) {
            return null;
        }
        
        return result;
    }
    
    public List<Participance> getParticpipancesByProject(Project project) {
        List<Participance> result;
        
        try {
            result = (ArrayList<Participance>)
                     em.createNamedQuery("ParticipancesByProject")
                     .setParameter("project", project)
                     .getResultList();
            
        } catch (NoResultException ex) {
            return null;
        }
        
        return result;
    }
    
}
